package com.pingan; /**
 * Alipay.com Inc.
 * Copyright (c) 2005-2010 All Rights Reserved.
 */


import com.pingan.mobilegw.adapterservice.ResultEnum;
import com.pingan.mobilegw.biz.shared.router.ServiceRouter;
import com.pingan.mobilegw.core.model.GatewayRequest;
import com.pingan.mobilegw.core.model.GatewayResponse;
import com.pingan.mobilegw.core.model.exception.MgwException;
import com.pingan.mobilegw.core.service.util.ResponseUtil;
import com.pingan.mobilegw.web.helper.RequestHelper;
import com.pingan.mobilegw.web.helper.ResponseHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.AnnotatedArrayType;


@Controller
@RequestMapping("/gateway")
public class GatewayController {

    /** 默认logger */
    private static final Log logger = LogFactory.getLog(GatewayController.class);


    /** 网关请求helper */
    @Autowired
    private RequestHelper        requestHelper;

    /** 响应请求helper */
    @Autowired
    private ResponseHelper       responseHelper;

    @Autowired
    private ServiceRouter serviceRouter;

    /**
     * GET请求处理
     *
     * @param httpServletResponse
     * @throws java.io.IOException
     */
    @RequestMapping(method = RequestMethod.GET)
    public void doGet(
            @RequestParam(value = "operationType", required = false) String operationType,
            @RequestParam(value = "requestData", required = false) String requestData,
            HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        doPost(operationType, requestData,httpServletRequest,httpServletResponse);
        System.out.println(operationType);
        System.out.println(requestData);
       // httpServletResponse.getWriter().write("Hello User.get");
        System.out.println("success");

    }


    /**
     * 网关入口
     *
     * @param httpServletRequest
     * @param httpServletResponse
     */
    @RequestMapping(method = RequestMethod.POST)
    public void doPost(
            @RequestParam(value = "operationType", required = false) String operationType,
            @RequestParam(value = "requestData", required = false) String requestData,
           HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse) throws IOException {

        String [] arr =SpringContextUtil.getApplicationContext().getBeanDefinitionNames();
        for(String s:arr){
            System.out.println(s);
        }
        System.out.println(operationType);
        System.out.println(requestData);
       // httpServletResponse.getWriter().write("Hello User.");
        GatewayRequest gatewayRequest = new GatewayRequest();
        GatewayResponse gatewayResponse = null;
        long startTime = System.currentTimeMillis();

        try {
            //====================1. 网关请求处理===================
             requestHelper.initRequest(gatewayRequest, httpServletRequest, httpServletResponse,
                 operationType, requestData);

            //====================2. 请求路由======================
              gatewayResponse = serviceRouter.processRoute(gatewayRequest);

        } catch (MgwException e) {
            logger.error("无线网关业务异常", e);
            gatewayResponse = ResponseUtil.buildErrorResponse(e);
        } catch (Throwable e) {
            logger.error("无线网关异常!", e);
            gatewayResponse = ResponseUtil.buildErrorResponse(ResultEnum.UnknowError);
        } finally {
            long elapsedTime = System.currentTimeMillis() - startTime;
            //TODO  SystemModel loggerInfo = mdapLogService.build("GatewayController.doPost", elapsedTime,
            //    gatewayRequest, gatewayResponse);
            //TODO mdapLogService.log(loggerInfo);
        }

        //====================3. 网关响应处理===================
        if (gatewayResponse != null) {
             responseHelper.handleResponse(gatewayRequest, gatewayResponse);
        }
    }

}
